package com.zby.business.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.zby.business.R;

/**
 * Created by BY on 2015/12/28.
 */
public class OnSaleFragment extends Fragment {
    private View view;
    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HorizontalScrollView scrollView;
    private WindowManager windowManager;
    int width;
    int tripWidth = 180;
    private String[] list = {"海鲜", "水果", "蔬菜", "零食", "坚果", "家电", "肉类", "数码", "熟食", "服装",};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.cuxiao, null);
        initView();
        setLinsoner();
        return view;
    }

    private void setLinsoner() {
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                for (int i = 0; i < list.length; i++) {
                    if (list[i] == tabId) {
                        mViewPager.setCurrentItem(i);
                        break;
                    }
                }
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabHost.setCurrentTab(position);
                updateTabHost();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateTabHost() {
        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            if (mTabHost.getCurrentTab() == i) {
                tv.setTextColor(Color.WHITE);
                if (tripWidth * i-1 > width + scrollView.getScrollX()) {
                    int n = (tripWidth * i - width) / tripWidth + 2;
                    scrollView.smoothScrollTo(n * tripWidth, 0);
                } else if (tripWidth * i < scrollView.getScrollX()) {
                    scrollView.smoothScrollTo(tripWidth * i, 0);
                }
            } else {
                tv.setTextColor(Color.parseColor("#DFDFDF"));
            }
        }
    }

    private void initView() {
        windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        width = windowManager.getDefaultDisplay().getWidth();
        scrollView = (HorizontalScrollView) view.findViewById(R.id.cuxiaoScrollView);
        mTabHost = (TabHost) view.findViewById(R.id.TabHost);
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager1);
        MyPagerAdapter pagerAdapter = new MyPagerAdapter();
        mViewPager.setAdapter(pagerAdapter);
        mTabHost.setup();
        TabWidget tabWidget = mTabHost.getTabWidget();
        mTabHost.getTabWidget().setStripEnabled(false);
        mTabHost.getTabWidget().setDividerDrawable(android.R.color.transparent);
        for (int i = 0; i < list.length; i++) {
            mTabHost.addTab(mTabHost.newTabSpec(list[i]).setIndicator(list[i], getResources().getDrawable(R.mipmap.axis)).setContent(R.id.viewPager1));
        }
        for (int i = 0; i < list.length; i++) {
            View view = tabWidget.getChildTabViewAt(i);
            view.getLayoutParams().width = tripWidth;
        }
        updateTabHost();
        mTabHost.setCurrentTab(1);
        mTabHost.setCurrentTab(0);
    }

    class MyPagerAdapter extends PagerAdapter {
        private View view;
        private LayoutInflater inflater;
        private TextView textView;

        public MyPagerAdapter() {
            inflater = LayoutInflater.from(getContext());
        }

        @Override
        public int getCount() {
            return list.length;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            view = inflater.inflate(R.layout.textview, null);
            textView = (TextView) view.findViewById(R.id.textView);
            textView.setText(list[position]);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
